//
//  Memory-Alignment-Demo.swift
//  Underlying-MemoryManagement
//
//  Created by jianbo.pei on 2023/12/11.
//

/**
 ## 参考链接
 - [Swift内存布局与指针操作](https://www.cnblogs.com/zbblog/p/15125741.html)
 */
import Foundation

func test_sizeof() {
//    test1()
    test_struct()
}

private func test1() {
    print(UInt8.bitWidth)
    print(UInt32.bitWidth)
    
    print(MemoryLayout<UInt8>.size)
    print(MemoryLayout<UInt8>.alignment)
    print(MemoryLayout<UInt8>.stride)
    
    print(MemoryLayout<Int32>.size)
    print(MemoryLayout<Int32>.alignment)
    print(MemoryLayout<Int32>.stride)
    
    /**
     8
     32
     1
     1
     1
     4
     4
     4
     */
}

struct MyStruct {
    let a: UInt8 = 0
    let b: Int32 = 0
    let c: UInt8 = 0
}

private func test_struct() {
    print(MemoryLayout<MyStruct>.size)
    print(MemoryLayout<MyStruct>.alignment)
    print(MemoryLayout<MyStruct>.stride)
    /**
     struct MyStruct {
         let a: UInt8 = 0
         let b: Int32 = 0
     }
     
     8
     4
     8
     */
    
    /**
     struct MyStruct {
         let b: Int32 = 0
         let a: UInt8 = 0
     }
     
     5
     4
     8
     */
    
    /**
     struct MyStruct {
         let b: Int32 = 0
         let a: UInt8 = 0
         let c: UInt8 = 0
     }
     
     6
     4
     8
     */
    
    /**
     struct MyStruct {
         let a: UInt8 = 0
         let c: UInt8 = 0
         let b: Int32 = 0
     }
     
     8
     4
     8
     */
    
    
    /**
     struct MyStruct {
         let a: UInt8 = 0
         let b: Int32 = 0
         let c: UInt8 = 0
     }
     
     9
     4
     12
     */
    
}


